package com.woniuxy.controller;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import org.apache.shiro.session.Session;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.woniuxy.dto.PageDto;
import com.woniuxy.entity.Orders;
import com.woniuxy.entity.User;
import com.woniuxy.service.LogService;
import com.woniuxy.service.OrdersService;
import com.woniuxy.utils.Result;

@RestController
@RequestMapping("/order")
public class OrdersController {

	@Autowired
	private OrdersService ordersService;
	@Autowired
	private LogService logservice;

	// @RequiresRoles(value ={"admin"},logical = Logical.OR)
	// @RequiresPermissions(value = {"student:del"},logical = Logical.AND)
	@RequestMapping("/findYesterday")
	public Result<Object> findYesterday() {
		Result result = new Result().setMessage(Result.SUCCESS);
		result.setData(ordersService.findYesterday());

		return result;
	}

	// @RequiresRoles(value ={"admin"},logical = Logical.OR)
	@RequestMapping("/findSomeday")
	public Result<Object> findSomeday(Date time) {

		Result result = new Result().setMessage(Result.SUCCESS);
		result.setData(ordersService.findSomeday(time));

		return result;
	}

	// @RequiresRoles(value ={"admin"},logical = Logical.OR)
	@RequestMapping("/findTimeFrame")
	public Result<Object> findTimeFrame(Date max_time, Date min_time) {

		Result result = new Result().setMessage(Result.SUCCESS);
		result.setData(ordersService.findTimeFrame(max_time, min_time));

		return result;
	}

	// @RequiresRoles(value ={"buyer","seller"},logical = Logical.OR)
	@RequestMapping(value = "/findOrdersByUid")
	public Result<Object> findOrdersByUid(int u_id, int pageNum, int pageSize) {
		Result result = new Result().setMessage(Result.SUCCESS);
		result.setData(ordersService.findOrdersByUid(u_id, pageNum, pageSize));

		return result;
	}

	// @RequiresRoles(value ={"buyer","seller"},logical = Logical.OR)
	@RequestMapping(value = "/addOrders", method = RequestMethod.POST)
	public Result<Object> addOrders(int p_id, int u_id, BigDecimal auction, BigDecimal service_charge,
			BigDecimal total_money) {
		Result result = new Result();
		if (ordersService.addOrders(p_id, u_id, auction, service_charge, total_money)) {
			result.setMessage(Result.SUCCESS);
		} else {
			result.setMessage(Result.ERROR);
		}

		return result;
	}

	// 通过用户id查找所有的订单
	@RequestMapping("/find")
	public PageDto<Orders> find(int page) {
		Subject currentUser = SecurityUtils.getSubject();
		Session session = currentUser.getSession();
		User user = (User) session.getAttribute("user");

		PageDto<Orders> orders = ordersService.find(user.getU_id(), page);
		System.out.println("sdasgreg" + orders);
		return orders;

	}

	// 根据订单id删除订单
	@RequestMapping("/del")
	public int del(int o_id, String order_no) {
		System.out.println("odedasfas" + order_no);

		// 添加日志信息
		logservice.addorderLogbuyer(order_no);

		return ordersService.del(o_id);

	}

	// 查找卖家订单
	@RequestMapping("/findsellerorders")
	public PageDto<Orders> findsellerorders(int page) {
		Subject currentUser = SecurityUtils.getSubject();
		Session session = currentUser.getSession();
		User user = (User) session.getAttribute("user");

		PageDto<Orders> orders = ordersService.findsellerorders(user.getU_id(), page);
		return orders;

	}

	// 查找订单详情
	@RequestMapping("/findorderdetail")
	public HashMap<String, Object> findorderdetail(int orderid) {
		Subject currentUser = SecurityUtils.getSubject();
		Session session = currentUser.getSession();
		User user = (User) session.getAttribute("user");

		return ordersService.findorderdetail(orderid, user.getU_id());

	}

}
